﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        Bank bank = new Bank();
        Account account;
        int flag = 0;//flag=1表示存钱按钮按下 flag=2表示取钱按钮按下
        public class Account//账号类
        {
            double money;
            string id;
            string pwd;
            public Account(string id, string pwd, double money)
            {
                //if( money < 0 ) throw new Exception("....");q
                this.id = id;
                this.pwd = pwd;
                this.money = money;
            }
            public double getMoney()
            {
                return money;
            }
            public void setMoney(double val)
            {
                this.money = val;
            }
            public string getId()
            {
                return id;
            }
            public void setId(string id)
            {
                this.id = id;
            }
            public string getpwd()
            {
                return pwd;
            }
            public void setPwd(string pwd)
            {
                this.pwd = pwd;
            }
            public bool SaveMoney(double money)
            {
                if (money < 0) return false; //卫语句
                this.money += money;
                return true;
            }
            public bool WithdrawMoney(double money)
            {
                if (this.money >= money)
                {
                    this.money -= money;
                    return true;
                }
                return false;
            }
            public bool IsMatch(string id, string pwd)
            {
                return id == this.id && pwd == this.pwd;
            }
        }
        public class Bank
        {
            List<Account> accounts = new List<Account>();
           
            public Account OpenAccount(string id, string pwd, double money)
            {
                Account account = new Account(id, pwd, money);
                accounts.Add(account);
                return account;
            }
           
            public bool CloseAccount(Account account)
            {
                int idx = accounts.IndexOf(account);
                if (idx < 0) return false;
                accounts.Remove(account);
                return true;
            }
      

            public Account FindAccount(string id, string pwd)
            {
                foreach (Account account in accounts)
                {
                    if (account.IsMatch(id, pwd))
                    {
                        return account;
                    }
                }
                return null;
            }
       
        }
        //BadCashException类，判断是否输入的全部是数字，否则抛出异常！
        class BadCashException
        {
            public static void findNotNumber(string s)
            {
                double a=0;
                if (double.TryParse(s, out a) == true)
                {

                }
            }
        }
        public Form1()
        {
            InitializeComponent();

            bank.OpenAccount("2222", "2222", 20);
            bank.OpenAccount("3333", "3333", 50);         
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.textBox1.Text = "请输入正确的账号！";
            this.textBox2.Text = "请输入正确的密码！";
            loged_false();
            saveOrDraw_false(); 
        }
        public void loging_false()
        {
            this.textBox1.Visible = false;
            this.textBox2.Visible = false;
            this.label1.Visible = false;
            this.label2.Visible = false;
            this.button1.Visible = false;
        }
        public void loging_true()
        {
            this.textBox1.Visible = true;
            this.textBox2.Visible = true;
            this.label1.Visible = true;
            this.label2.Visible = true;
            this.button1.Visible = true;
        }
        public void loged_false() 
        {
            this.button2.Visible = false;
            this.button3.Visible = false;
            this.button4.Visible = false;
            this.label3.Visible = false; 
            this.button6.Visible = false;
        }
        public void loged_true()
        {
            this.button2.Visible = true;
            this.button3.Visible = true;
            this.label3.Visible = true;
            this.button4.Visible = true;
            this.button6.Visible = true;
        }
        public void saveOrDraw_false() 
        {
            this.textBox3.Visible = false;
            this.button5.Visible = false;
            this.label4.Visible = false;
        }
        public void saveOrDraw_true()
        {
            this.textBox3.Visible = true;
            this.button5.Visible = true;
            this.label4.Visible = true;
        }
        //登录界面textBox1，textBox2，button1，label1，label2
       

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            account = bank.FindAccount(this.textBox1.Text, this.textBox2.Text);
            if (account == null)
            {
                MessageBox.Show("密码或者账号输入不正确!");
            }
            else 
            {
                loging_false();
                loged_true();
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

       
        //button2：显示余额 Label3：显示信息 button3：退出按钮 button4:存钱 label4:点击存取钱才显示信息 
        //textbox3:存取钱文本框 button5:确定按钮 button6:取钱
        private void label3_Click(object sender, EventArgs e)
        {
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.label3.Text = ("余额：" + account.getMoney().ToString());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            loged_false();
            saveOrDraw_false();
            this.textBox2.Text = "";
            loging_true();
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            saveOrDraw_true();
            flag = 1;
        }
        private void label4_Click(object sender, EventArgs e)
        {

        }
        private void textBox3_TextChanged(object sender, EventArgs e)
        {
           
        }
        
        private void button5_Click(object sender, EventArgs e)
        {
            if (flag == 1) 
            {  //捕获异常输入
                try
                {
                    BadCashException.findNotNumber(textBox3.Text);
                    account.SaveMoney(double.Parse(textBox3.Text));
                    this.textBox3.Text = "";
                    saveOrDraw_false();
                }
                catch (Exception a)
                {
                    MessageBox.Show("输入不全是数字！");
                }
            }
            if (flag == 2)
            {
                try
                {
                    BadCashException.findNotNumber(textBox3.Text);
                    if (account.WithdrawMoney(double.Parse(textBox3.Text)) != false)
                    {
                        saveOrDraw_false();
                    }
                    else if (account.getMoney() < double.Parse(textBox3.Text))
                    {
                        MessageBox.Show("余额不足，请重新输入");
                    }
                    this.textBox3.Text = "";
                }
                catch (Exception a) 
                {
                    //在这里就不需要显示异常信息，因为在drawBigMoney事件中已经触发了异常显示信息，只需
                    //要有异常捕获，不至于让程序崩溃就行
                }
                
            }
            this.label3.Text = ("余额：" + account.getMoney().ToString());
        }

        private void button6_Click(object sender, EventArgs e)
        {
            saveOrDraw_true();
            flag = 2;
        }
        Form frm = new Form();
        TextBox tbox = new TextBox();
        //取钱金额大于等于10000时，将会触发事件drawBigMoney。特别注意的是该事件要在Form1.Designer.cs中的
        //this.button5.Click += new System.EventHandler(this.button5_Click);这一句前面注册。用不到委托。
        private void drawBigMoney(object sender, EventArgs e) 
        { 
           try
           {
              BadCashException.findNotNumber(textBox3.Text);
                if (flag == 2)
                {
                    double bigMoney = 0;
                    bigMoney = double.Parse(textBox3.Text);
                    if (bigMoney >= 10000)
                    {
                        string input;
                        frm.Size = new System.Drawing.Size(250,250);
 
                        Label lab = new Label();
                        lab.AutoSize = true;
                        lab.Text = "取款的金额大于等于10000，请再次输入密码！点击右下方按钮直接退出！";
                      
                        tbox.Text = "请输入密码！";

                        Button btn_submit = new Button();
                        btn_submit.Text = "确定";
                    
                        Button btn_exit = new Button();
                        btn_exit.Text="退出";
                        //在frm中加入其它控件
                        frm.Controls.Add(lab);
                        frm.Controls.Add(tbox);
                        frm.Controls.Add(btn_submit);
                        frm.Controls.Add(btn_exit);
                        //划定控件在frm中的位置
                        lab.Location = new System.Drawing.Point(0, 12);
                        tbox.Location = new System.Drawing.Point(10, 50);
                        btn_submit.Location = new System.Drawing.Point(80,100);
                        btn_exit.Location = new System.Drawing.Point(150, 180);
                        //注册事件
                        btn_submit.Click += new System.EventHandler(this.btn_submit_Click);
                        btn_exit.Click += new System.EventHandler(this.btn_exit_Click);

                        if (frm.ShowDialog() == DialogResult.OK)
                        {
                            input = tbox.Text;   
                        }
                    }
                 }
              }
            catch (Exception a)
            {
                MessageBox.Show("输入不全是数字！");
            }
        }
        private void btn_submit_Click(object sender, EventArgs e)
        {

            if (String.Compare(tbox.Text, account.getpwd()) == 0)
            {
                frm.Close();
            }
            else if (String.Compare(tbox.Text, account.getpwd()) != 0)
            {
                flag = 0;
                MessageBox.Show("输入的密码不正确！");
                frm.Close();
                loged_false();
                saveOrDraw_false();
                this.textBox2.Text = "";
                loging_true();
            }

        }
        private void btn_exit_Click(object sender, EventArgs e)
        {
            flag = 0;
            frm.Close();
            loged_false();
            saveOrDraw_false();
            this.textBox2.Text = "";
            loging_true();
        }
    }
}
